package com.zmn.plat.admin.controller.service.itempart;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.service.itempart.ItemPart;
import com.zmn.plat.services.interfaces.service.itempart.ItemPartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 项目-关联配件基础修改Controller
 *
 * @author guoqiao
 * @since 2021/11/19 19:07
 */
@Slf4j
@RestController
@RequestMapping("/service/itempart")
public class ItemPartModifyController extends ServiceItemPartBaseController {

    private ItemPartService itemPartService;

    /**
     * 描述: 保存
     *
     * @author guoqiao
     * @since 2021/11/19 19:07
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ItemPart itemPart) {

        Date now = DateUtil.getNow();
        int relateId = itemPart.getRelateId();
        try {
            itemPart.setUpdater(getMcStaffRealName());
            itemPart.setUpdateTime(now);
            if (NumberUtil.isNullOrZero(relateId)) {
                itemPart.setCreater(getMcStaffRealName());
                itemPart.setCreateTime(now);
                itemPartService.insert(itemPart);
            } else {
                itemPartService.updateByKey(itemPart);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 删除
     * @param relateId 主键
     *
     * @author guoqiao
     * @since 2021/11/19 19:07
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "relateId") Integer relateId) {

        try {
            if (NumberUtil.isNullOrZero(relateId)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            itemPartService.deleteByKey(relateId);
            return ResponseDTO.success("删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

}
